/**
 * 系统后台数据库连接配置JS-systemDataSource.jsp
 */

//项目路径
var strPath = window.document.location.pathname;
//var postPath = strPath.substring(0, strPath.substr(1).indexOf('/') + 1);
var postPath = '/gdp';
var editStart = 1; //1添加、2修改
$(function () {
    $('.portlet-body').css({"minHeight": $(window).height() - 90 + 'px', 'overflowY': 'auto'});
    getServerType();
    getProjectSelect();
    $('#hid_a_pcode').val(GetQueryString('a_pcode'));

    //默认选中的项目工程
    if ($('#hid_a_pcode').val() != 'null') {
        $('#ptype').val($('#hid_a_pcode').val());
    }
    retrunIndex();
    //数据列表
    $('#tb_dataList').bootstrapTable({
        url: postPath + '/system/sysPageBaseInfo/getPageBaseInfoList',
        undefinedText: '-',
        pagination: true, // 分页
        striped: true, // 是否显示行间隔色
        queryParams: queryParams,
        cache: false, // 是否使用缓存
        pageList: [5, 10, 20],
        toolbar: '', // 指定工具栏
        showColumns: false, // 显示隐藏列
        showRefresh: false, // 显示刷新按钮
        uniqueId: 'pagecode', // 每一行的唯一标识
        sidePagination: 'server', // 服务端处理分页
        responseHandler: function (res) {
            return {
                total: res.data.total, //总页数
                rows: res.data.rows, //数据
            };
        },
        columns: [
            {
                field: 'state',
                checkbox: true,
                align: 'center',
                valign: 'middle',
            },
            {
                title: '页面编码',
                field: 'pagecode', // 字段
                align: 'center', // 对齐方式（左 中 右）
                valign: 'middle', //
                sortable: false,
            },
            {
                title: '查询服务名称',
                field: 'searchservicename',
                align: 'center',
                valign: 'middle',
                sortable: false,
            },
            {
                title: '编辑服务名称',
                field: 'editservicename',
                align: 'center',
                valign: 'middle',
                sortable: false,
                formatter: function (value, row, index) {
                    if (row.enable == 0) {
                        value = '-';
                        row.editservicename = value;
                    }
                    return value;
                },
            },
            {
                title: '是否启用编辑',
                field: 'enable',
                align: 'center',
                valign: 'middle',
                sortable: false,
                formatter: function (value, row, index) {
                    if (value == 1) {
                        value = '是';
                    } else {
                        value = '否';
                    }
                    return value;
                },
            },
            {
                title: '服务类型',
                field: 'servicetypename',
                align: 'center',
                valign: 'middle',
                sortable: false,
            },
            {
                title: '所属项目',
                field: 'pname',
                align: 'center',
                valign: 'middle',
                sortable: false,
            },
            {
                title: '操作',
                align: 'center',
                valign: 'middle',
                sortable: false,
                formatter: function (value, row, index) {
                    var strtd = '';
                    //strtd+="<a href='javascript:;' onclick='addClick();' class='btn btn-default btn-sm'><i class='fa fa-plus'></i></a>";
                    strtd +=
                        "<a href='javascript:;' title='编辑' onclick='updatepServerClick(\"" +
                        row.pagecode +
                        '","' +
                        row.pcode +
                        '","' +
                        row.servicetype +
                        '","' +
                        row.searchserviceid +
                        '","' +
                        row.enable +
                        '","' +
                        row.editserviceid +
                        "\");' class='btn btn-default btn-sm'><i class='fa fa-edit'></i> </a> ";
                    strtd +=
                        " <a href='javascript:;' title='删除' onclick='deleteServerClick(\"" +
                        row.pagecode +
                        "\");' class='btn btn-default btn-sm'><i class='fa fa-times'></i> </a>";
                    strtd +=
                        " <a href='javascript:;' title='预览' onclick='previewServerClick(\"" +
                        row.servicetype +
                        '","' +
                        row.pagecode +
                        "\");' class='btn btn-default btn-sm'><i class='fa fa-send'></i> </a>";
                    return strtd;
                },
            },
        ],
    });
    //查询操作
    $('#btn_Search').click(function () {
        $('#tb_dataList').bootstrapTable('refresh');
    });

    //刷新操作
    $('#btn_ref').click(function () {
        $('#tb_dataList').bootstrapTable('refresh');
    });

    //保存数据-添加按钮
    $('#btn_add').click(function () {
        $(window.parent.document)
            .find('#sysIframe')
            .attr('src', 'pageDetailsInfo/index');
    });
});

/*************加载下拉框***********************/
function retrunIndex() {
    $('#ptype').val(GetQueryString('pcode'));
    $('#servertype').val(GetQueryString('servertype'));
    if (GetQueryStringSeachTxt('seachTxt') != null) {
        if (GetQueryStringSeachTxt('seachTxt') != 'null') {
            $('#seachTxt').val(decodeURI(GetQueryStringSeachTxt('seachTxt')));
        }
    }
}

function getProjectSelect() {
    $('#ptype').html('');
    $.ajax({
        type: 'POST',
        // url: postPath + '/system/sysprjService/getProjectList',
        url: postPath + "/system/sysProject/getProjectToUser",
        async: false,
        data: '',
        dataType: 'json',
        success: function (result) {
            if (result != null) {
                var str = "<option value=''>请选择</option>";
                if (result.success && result.data != null && result.data.length > 0) {
                    for (var i = 0; i < result.data.length; i++) {
                        str += "<option value='" + (result.data[i].id || result.data[i].pcode) + "'>" + (result.data[i].name || result.data[i].pname) + "</option>";
                    }
                }
                $('#ptype').append(str);
                $('#projectcode').html(str);
            }
        },
        error: function () {
            // bootbox.alert('保存失败,请刷新后重试!');
            showMsg("保存失败,请刷新后重试!", "error");
        },
    });
}

function getServerType() {
    $.ajax({
        type: 'POST',
        url: postPath + '/system/sysprjService/getServerType',
        async: false,
        data: '',
        dataType: 'json',
        success: function (res) {
            if (null != res) {
                var str = '';
                var server = "<option value=''>请选择</option>";
                for (var i = 0; i < res.length; i++) {
                    if (
                        res[i].servicename == '单表查询服务' ||
                        res[i].servicename == '脚本查询服务'
                    ) {
                        str +=
                            "<label><input type='radio' name='servicetype' class='icheck' value='" +
                            res[i].guid +
                            "'> " +
                            res[i].servicename +
                            '</label>';
                        server +=
                            "<option value='" +
                            res[i].guid +
                            "'>" +
                            res[i].servicename +
                            '</option>';
                    }
                }
                $('#serverList').html(str);
                $('#servertype').html(server);
            }
        },
    });
}

//选择系统下的服务
$('#ptype').change(function () {
    $('#tb_dataList').bootstrapTable('refresh');
});

/**查询条件与分页数据 */
function queryParams(pageReqeust) {
    var temp = {
        order: pageReqeust.order,
        pageNo: pageReqeust.offset / pageReqeust.limit + 1,
        pageSize: pageReqeust.limit,
        seachTxt: $('#seachTxt').val(),
        pcode: $('#ptype').val(),
        servertype: $('#servertype').val(),
    };
    return temp;
}

function deleteServerClick(pagecode) {
    bootbox.confirm('确认删除?', function (result) {
        if (result == false) return;
        $.ajax({
            url: postPath + '/system/sysPageBaseInfo/deletePageBaseInfo',
            type: 'POST',
            data: {pagecode: pagecode},
            success: function (res) {
                if (res != 0) {
                    // bootbox.alert('删除成功!');
                    showMsg("删除成功!", "success");
                    $('#tb_dataList').bootstrapTable('refresh');
                } else {
                    // bootbox.alert('删除失败!');
                    showMsg("删除失败!", "error");
                }
            },
            error: function () {
                // bootbox.alert('删除失败!');
                showMsg("删除失败!", "error");
            },
        });
    });
}

function previewServerClick(servicetype, pagecode) {
    window.open(
        postPath +
        '/preview/PageInfoContent.html?servicetype=' +
        servicetype +
        '&pageCode=' +
        pagecode +
        ''
    );
}

function updatepServerClick(
    pagecode,
    pcode,
    servicetype,
    searchserviceid,
    enable,
    editserviceid
) {
    var seachTxt = $('#seachTxt').val();
    var url = encodeURI(
        'pageDetailsInfo/index' +
        '?pagecode=' +
        pagecode +
        '&servertype=' +
        servicetype +
        '&projectid=' +
        pcode +
        '&searchserviceid=' +
        searchserviceid +
        '&enable=' +
        enable +
        '&editserviceid=' +
        editserviceid +
        '&seachTxt=' +
        seachTxt
    );
    $(window.parent.document)
        .find('#sysIframe')
        .attr('src', url);
}

function GetQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

function GetQueryStringSeachTxt(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return r[2];
    return null;
}

function showMsg(text, icon) {
    $.toast().reset('all');
    $.toast({
        text: text,
        icon: icon,
        showHideTransition: 'fade',
        allowToastClose: true,
        hideAfter: 1500,
        stack: false,
        position: 'top-right',
        textAlign: 'left',
        loader: true,
        loaderBg: '#9ec600',
    });
}
